#|
  This file is a part of cl-script project.
  Copyright (c) 2013 Mike Manilone (crtmike@gmx.us)
|#

(in-package :cl-user)
(defpackage cl-script-python3
  (:use :cl)
  (:import-from :cffi
                :define-foreign-library
                :use-foreign-library
                :defcfun)
  (:import-from :cl-script
                :script-engine))
(in-package :cl-script-python3)

(annot:enable-annot-syntax)

;;; FFI stuff
(define-foreign-library :python3 (:unix (:default "/usr/lib64/libpython3")))
(use-foreign-library :python3)

(defcfun "PyRun_SimpleString" :int
  (command :string))
(defcfun "Py_Initialize" :void)
(defcfun "Py_Finalize" :void)

;;; 
@export
(define-condition python-exception () ())

@export
(defclass python3-engine (script-engine)
  ())

(defmethod initialize-instance ((engine python3-engine) &rest args)
  (declare (ignore args))
  (py-initialize))

(defmethod ccl:terminate ((engine python3-engine))
  (py-finalize))

(defmethod cl-script:simple-execute ((mod python3-engine) code)
  (if (/= 0 (PyRun-SimpleString code))
      (error 'python-exception)))

